/**
 * @param {string[]} nums
 * @param {number[][]} queries
 * @return {number[]}
 */
var smallestTrimmedNumbers = function(nums, queries) {
  let result = [], length = nums[0].length, arr = [...nums];
  for(let i = 0; i < queries.length; i++) {
    arr.sort((a, b) => {
      if(queries[i][1] >= length) {
        if(parseInt(a) - parseInt(b) == 0) {
          const indexA = nums.indexOf(a), indexB = nums.indexOf(b);
          if(indexB == indexA) {
            return -1;
          }
          return indexA - indexB;
        }
        return parseInt(a) - parseInt(b);
      } else {
        let aVal = parseInt(a.slice(length - queries[i][1], length)), bVal = parseInt(b.slice(length - queries[i][1], length));
        if(aVal - bVal == 0) {
          const indexA = nums.indexOf(a), indexB = nums.indexOf(b);
          if(indexB == indexA) {
            return -1;
          }
          return indexA - indexB;
        }
        return aVal - bVal;
      }
    });
    // console.log(i, arr, queries[i]);
    const index = nums.indexOf(arr[queries[i][0] - 1]);
    result.push(index);
  }
  return result;
};
let nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]];
console.log(smallestTrimmedNumbers(nums, queries));
nums = ["24","37","96","04"], queries = [[2,1],[2,2]];
console.log(smallestTrimmedNumbers(nums, queries));
nums = ["64333639502","65953866768","17845691654","87148775908","58954177897","70439926174","48059986638","47548857440","18418180516","06364956881","01866627626","36824890579","14672385151","71207752868"], queries = [[9,4],[6,1],[3,8],[12,9],[11,4],[4,9],[2,7],[10,3],[13,1],[13,1],[6,1],[5,10]];
console.log(smallestTrimmedNumbers(nums, queries));
